<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>盒子跟随鼠标移动</title>
		<style type="text/css">
			#div1{
				width: 200px;
				height: 200px;
				background-color: #FF0000;
				position: absolute;
				top: 20px;
				left: 10px;
				right: 0;
			}
		</style>
	</head>
	<body>
		<div id="div1">
			
		</div>
		<script type="text/javascript">
		var div1 = document.getElementById('div1');
		var body = document.getElementsByTagName('html');
		div1.onmousemove = function(e){
			let event = e || window.event;
			var x = event.clientX;
			var y = event.clientY;
			div1.style.left = x -div1.offsetWidth/2+'px';
			div1.style.top = y-div1.offsetHeight/2+'px' ;
		}
			
	
		</script>
	</body>
</html>
